// pages/sou_suo/sou_suo.js
import {
  request
} from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    hostry: [],
    reso: []
  },
  back() {
    wx.navigateBack();
  },
  onCancel() {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (getApp().globalData.userInfo == null) {
      wx.navigateTo({
        url: '/pages/deng_lu/deng_lu',
      })
    }
    this.hostry();
    this.hot();
  },
  tohostry(e){
    console.log(e);
    this.onSearch({detail:e.currentTarget.id});
  },
  hot(){
    request({
      url:"/dianshang/chronicle/listhot",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      console.log(data);
      data.rows.forEach(item=>{
        item={
          text:item,
          width:0
        }
        item.width = item.text.length * 30 + 50 * 2;
        this.data.reso.push(item);
      })
      this.setData({
        reso:this.data.reso
      })
    })
  },
  hostry() {
    request({
      url: "/dianshang/chronicle/listcat?userId=" + getApp().globalData.userInfo.userId,
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data => {
      console.log(data);
      data.rows.forEach(item => {
        item = {
          name: item,
          width: 0
        };
        console.log(item);
        item.width = item.name.length * 30 + 50 * 2;
        this.data.hostry.push(item);
      })
      console.log(this.data.hostry);
      this.setData({
        hostry: this.data.hostry
      })
    })
  },
  onSearch(e) {
    console.log(e.detail);
    if (e.detail == "") {
      wx.reLaunch({
        url: "/pages/fen_lei/fen_lei?value=" + e.detail
      })
    } else {
      request({
        url: "/dianshang/chronicle",
        method: "POST",
        data: {
          userid: getApp().globalData.userInfo.userId,
          name: e.detail
        },
        header: {
          "authorization": getApp().globalData.authorization
        }
      }).then(data => {
        console.log(data);
        wx.reLaunch({
          url: "/pages/fen_lei/fen_lei?value=" + e.detail
        })
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})